﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.settings
{
    public partial class contents_edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString();     //操作类型
        private int id = 0;
        public int category_id;                                    //类别ID
        private int channel_id;                                //栏目ID
        public He.Model.article model;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }

                model = new BLL.article().GetModel(this.id);
                //获取父级信息
                He.Model.category cmodel = new BLL.category().GetModel(model.category_id);
                category_id = model.category_id;
                channel_id = cmodel.channel_id;
            }
            if (!Page.IsPostBack)
            {
                ddlCategoryBind();
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo();
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            txtName.Text = model.name;
            txtTitle.Text = model.title;
            ddlcategory_id.SelectedValue = category_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
            chkProtected.Checked = model.is_sys == 1 ? true : false;
            chkLock.Checked = model.is_lock == 1 ? true : false;
            txtSeoKeywords.Text = model.seo_keywords;
            txtSeoDescription.Text = model.seo_desc;
            txtContent.Value = model.content;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.article bll = new BLL.article();
            Model.article model = new Model.article();
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.category_id = int.Parse(ddlcategory_id.SelectedValue);
            model.content = txtContent.Value;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_sys = chkProtected.Checked ? 1 : 0;
            model.is_lock = chkLock.Checked ? 1 : 0;
            //自动提取SEO关健字
            if (txtSeoKeywords.Text.Trim() != string.Empty)
            {
                model.seo_keywords = txtSeoKeywords.Text.Trim();
            }
            else
            {
                model.seo_keywords = txtTitle.Text.Trim();
            }
            //自动提取SEO描述
            if (txtSeoDescription.Text.Trim() != string.Empty)
            {
                model.seo_desc = txtSeoDescription.Text;
            }
            else
            {
                model.seo_desc = He.Common.StringHelper.ClearHtmlTags(txtContent.Value, 250);
            }
            if (bll.Add(model) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            He.Model.article model = new He.BLL.article().GetModel(_id);
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.category_id = int.Parse(ddlcategory_id.SelectedValue);
            model.content = txtContent.Value;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_sys = chkProtected.Checked ? 1 : 0;
            model.is_lock = chkLock.Checked ? 1 : 0;
            //自动提取SEO关健字
            if (txtSeoKeywords.Text.Trim() != string.Empty)
            {
                model.seo_keywords = txtSeoKeywords.Text.Trim();
            }
            else
            {
                model.seo_keywords = txtTitle.Text.Trim();
            }
            //自动提取SEO描述
            if (txtSeoDescription.Text.Trim() != string.Empty)
            {
                model.seo_desc = txtSeoDescription.Text;
            }
            else
            {
                model.seo_desc = StringHelper.ClearHtmlTags(txtContent.Value, 250);
            }
            if (!new He.BLL.article().Update(model))
            {
                return false;
            }
            return true;

        }
        #endregion

        #region 类别绑定=================================
        private void ddlCategoryBind()
        {
            BLL.channel sbll = new BLL.channel();
            BLL.category cbll = new BLL.category();
            ddlcategory_id.Items.Add(new ListItem("请选择分类", ""));
            //绑定中文类别
            DataTable he_sys = sbll.GetList(string.Format("model_id={0} and lang='{1}'", (int)Enums.sys_model.内容模块, lang)).Tables[0];
            foreach (DataRow dr_sys in he_sys.Rows)
            {
                DataTable he_category = cbll.GetList(0, Convert.ToInt32(dr_sys["id"]));
                foreach (DataRow dr_category in he_category.Rows)
                {
                    string Id = dr_category["Id"].ToString();
                    int layer = int.Parse(dr_category["layer"].ToString());
                    string Title = dr_category["Title"].ToString().Trim();

                    if (layer == 1)
                    {
                        ddlcategory_id.Items.Add(new ListItem(Title, Id));
                    }
                    else
                    {
                        Title = "├ " + Title;
                        Title = StringHelper.Repeat("　", layer - 1) + Title;
                        ddlcategory_id.Items.Add(new ListItem(Title, Id));
                    }


                }
            }
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", "contents_list.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", "contents_list.aspx?lang=" + lang, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", "contents_list.aspx?lang=" + lang, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", "contents_list.aspx?lang=" + lang, "Success");
            }
        }
    }
}